<template>
<uni-shadow-root class="partials-custom_page-index"><view class="custom_page_container" :style="'margin-bottom:'+(footerImgState?'320rpx':'0')">
  <block v-for="(item,index) in (PaiXuPartials)">
    
    <component-tag-nameNearShop v-if="item.partialType == '23'" :receiveData="item"></component-tag-nameNearShop> 

    <component-tag-mofang v-if="item.partialType == '14'" :receiveData="item"></component-tag-mofang> 

    <component-tag-nametab id="tabPage" v-if="item.partialType == '13'" class="positionNotifyer" :receiveData="item"></component-tag-nametab> 

    <component-tag-nameBanner v-if="item.partialType =='7'&&showBannerState" :receiveData="item"></component-tag-nameBanner>

    <component-tag-name v-if="item.partialType =='4'" :id="'title_'+(index)" :receiveData="item"></component-tag-name>  

    <component-tag-nameImageLink v-if="item.partialType =='6'" :receiveData="item"></component-tag-nameImageLink>

    <component-tag-nameGridList v-if="item.partialType =='8'" :receiveData="item"></component-tag-nameGridList>

    <component-tag-nameHelpBlank v-if="item.partialType =='3'" :receiveData="item"></component-tag-nameHelpBlank>

    <component-tag-nameTextLink v-if="item.partialType =='5'" :receiveData="item"></component-tag-nameTextLink>

    <component-tag-namedetailList v-if="item.partialType =='9'" :receiveData="item"></component-tag-namedetailList>

    <component-tag-nameProductList v-if="item.partialType =='15'" :receiveData="item" @resEvent="resEventFun"></component-tag-nameProductList>

    <component-tag-nameNewsList v-if="item.partialType =='16'" :receiveData="item"></component-tag-nameNewsList>

    <component-tag-nameVideo v-if="item.partialType =='20'" :receiveData="item"></component-tag-nameVideo>

    <component-tag-nameAudio v-if="item.partialType =='21'" :data="item"></component-tag-nameAudio>
                
    <component-tag-nameCoupons v-if="item.partialType =='19'" :data="item"></component-tag-nameCoupons>

    <component-tag-nameActivitiesGoods v-if="item.partialType =='18'" :data="item"></component-tag-nameActivitiesGoods>
         
    <component-tag-nameActivitiesList v-if="item.partialType =='17'" :receiveData="item"></component-tag-nameActivitiesList>

    <component-tag-nameRichText v-if="item.partialType =='1'" :receiveData="item"></component-tag-nameRichText>

    <component-tag-nameStores v-if="item.partialType =='22'" :data="item"></component-tag-nameStores>

    <component-tag-organizesList v-if="item.partialType =='25'" :data="item"></component-tag-organizesList>

    <component-tag-formCommitList v-if="item.partialType =='26'" :receiveData="item"></component-tag-formCommitList>

    <component-tag-spaceList v-if="item.partialType =='27'" :data="item"></component-tag-spaceList>
    
    <component-tag-locateAddress id="locateAddress" v-if="item.partialType =='28'" :receiveData="item"></component-tag-locateAddress>

    <component-tag-shoppingCar v-if="item.partialType =='29'" :receiveData="item"></component-tag-shoppingCar>

    <component-tag-jifenList v-if="item.partialType =='30'" :data="item"></component-tag-jifenList>
    
    <component-tag-servantList v-if="item.partialType =='32'" :receiveData="item"></component-tag-servantList>
    
    <component-tag-courseList v-if="item.partialType =='33'" :receiveData="item"></component-tag-courseList>
    
    <component-tag-sectionList v-if="item.partialType =='34'" :receiveData="item"></component-tag-sectionList>
    
    <component-tag-nearMendianList v-if="item.partialType =='35'" :receiveData="item"></component-tag-nearMendianList>
    
    <component-task-list v-if="item.partialType =='36'" :receiveData="item"></component-task-list>
    
    <component-assistance-list v-if="item.partialType =='37'" :receiveData="item"></component-assistance-list>
  

  </block>
  <cover-view :style="'position:fixed;top:'+(defaultTop)+'px;right:20rpx;z-index:10;'">
    <block v-for="(item,index) in (PaiXuPartials)" :key="item">
      <component-tag-kefu v-if="item.partialType =='24'" :receiveData="item"></component-tag-kefu>
    </block>
  </cover-view>
  
  
  <view class="authorization" v-if="showUserForm" @touchmove.stop.prevent="true">
    <view class="authorizationBg"></view>
    <view class="popupPage">
      <scroll-view scroll-y style="height:100%;">
        <component-form-detail id="userForm" v-if="sendOptionData&&showAddressForm" :receiveData="sendOptionData" showTitle="false" showBtn="false" :userAddressCustomFormCommitId="userInfoFormCommitId" @sendDataFun="getDataFun"></component-form-detail>
      </scroll-view>
      <view class="submitBtn" :style="'background:'+(setting.platformSetting.defaultColor)" @click.stop.prevent="submitData">提交资料</view>
    </view>
  </view>
  
  <view v-if="showPopup">
    <view class="authorizationBg"></view>
    <view class="authorization">
      <view class="authorizationPopup">
        <view class="tipContent">
          <text style="font-size:38rpx;">前往微信授权</text>
        </view>
        <view class="buttonArray">
          <button class="button cancel" @click="cancel">取消</button>
          <button class="button confirm" open-type="getUserInfo" :style="'color:'+(setting.platformSetting.defaultColor)" @getuserinfo="bindGetUserInfo">确定</button>
        </view>
      </view>
    </view>
  </view>

</view></uni-shadow-root>
</template>

<script>
import ComponentTagName from '../title/title.vue'
import ComponentTagNameBanner from '../banner/banner.vue'
import ComponentTagNameImageLink from '../image_link/image_link.vue'
import ComponentTagNameGridList from '../gridlist/gridlist.vue'
import ComponentTagNameHelpBlank from '../help_blank/help_blank.vue'
import ComponentTagNameTextLink from '../text_link/text_link.vue'
import ComponentTagNamedetailList from '../detailList/detailList.vue'
import ComponentTagNameProductList from '../productList/productList.vue'
import ComponentTagNameNearShop from '../nearShop/nearShop.vue'
import ComponentTagNameNewsList from '../newsList/newsList.vue'
import ComponentTagNameCoupons from '../coupons/coupons.vue'
import ComponentTagNameActivitiesGoods from '../activitiesGoods/activitiesGoods.vue'
import ComponentTagNameActivitiesList from '../activitiesList/activitiesList.vue'
import ComponentTagNameRichText from '../richText/richText.vue'
import ComponentTagNameStores from '../stores/stores.vue'
import ComponentTagNametab from '../tab/index.vue'
import ComponentTagMofang from '../mofang/index.vue'
import ComponentTagKefu from '../kefu/kefu.vue'
import ComponentTagLocateAddress from '../locateAddress/index.vue'
import ComponentTagSpaceList from '../space_list/index.vue'
import ComponentTagOrganizesList from '../organizes_list/index.vue'
import ComponentTagCourseList from '../courseList/index.vue'
import ComponentTagSectionList from '../section_list/index.vue'
import ComponentTagServantList from '../servant_list/index.vue'
import ComponentTagNearMendianList from '../near_mendain_list/index.vue'
import ComponentTagFormCommitList from '../form_commit_list/index.vue'
import ComponentTagShoppingCar from '../shopping_car/index.vue'
import ComponentTagJifenList from '../jifen_list/index.vue'
import ComponentFooter from '../footer/index.vue'
import ComponentFormDetail from '../form_detail/index.vue'
import ComponentAssistanceList from '../assistance_list/index.vue'
import ComponentTaskList from '../task_list/index.vue'
import ComponentTagNameVideo from '../video/video.vue'
import ComponentTagNameAudio from '../audio/audio.vue'
global['__wxVueOptions'] = {components:{'component-tag-name': ComponentTagName,'component-tag-nameBanner': ComponentTagNameBanner,'component-tag-nameImageLink': ComponentTagNameImageLink,'component-tag-nameGridList': ComponentTagNameGridList,'component-tag-nameHelpBlank': ComponentTagNameHelpBlank,'component-tag-nameTextLink': ComponentTagNameTextLink,'component-tag-namedetailList': ComponentTagNamedetailList,'component-tag-nameProductList': ComponentTagNameProductList,'component-tag-nameNearShop': ComponentTagNameNearShop,'component-tag-nameNewsList': ComponentTagNameNewsList,'component-tag-nameCoupons': ComponentTagNameCoupons,'component-tag-nameActivitiesGoods': ComponentTagNameActivitiesGoods,'component-tag-nameActivitiesList': ComponentTagNameActivitiesList,'component-tag-nameRichText': ComponentTagNameRichText,'component-tag-nameStores': ComponentTagNameStores,'component-tag-nametab': ComponentTagNametab,'component-tag-mofang': ComponentTagMofang,'component-tag-kefu': ComponentTagKefu,'component-tag-locateAddress': ComponentTagLocateAddress,'component-tag-spaceList': ComponentTagSpaceList,'component-tag-organizesList': ComponentTagOrganizesList,'component-tag-courseList': ComponentTagCourseList,'component-tag-sectionList': ComponentTagSectionList,'component-tag-servantList': ComponentTagServantList,'component-tag-nearMendianList': ComponentTagNearMendianList,'component-tag-formCommitList': ComponentTagFormCommitList,'component-tag-shoppingCar': ComponentTagShoppingCar,'component-tag-jifenList': ComponentTagJifenList,'component-footer': ComponentFooter,'component-form-detail': ComponentFormDetail,'component-assistance-list': ComponentAssistanceList,'component-task-list': ComponentTaskList,'component-tag-nameVideo': ComponentTagNameVideo,'component-tag-nameAudio': ComponentTagNameAudio}}

global['__wxRoute'] = 'partials/custom_page/index'
const app = getApp();
Component({
  name:"customPage",
  properties: {
    receiveData: {
      type: String,
      value: 'default value',
    },
    showFooter: {
      type: String,
      value: '',
    },
  },
  data: {
    // 这里是一些组件内部数据
    showPopup:false,
    renderData: null,
    PaiXuPartials:null, 
    kefuCount: 0,
    footerCount: 0,
    authorizationCount:0,
    defaultTop:0,
    footerImgState: false,
    sendOptionData: null,
    showAddressForm: false,
    userInfoFormCommitId: '',
    showUserForm: false,
    showFooterState:true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    showBannerState:true,
    paramData:null,
  },
  ready: function () {
      this.initFun();
  },
  methods: {
    initFun:function(params){
        let that = this;
        app.globalData.footerCount++;
        app.globalData.authorizationCount++;
        let paramData=params?params:that.data.receiveData;
        that.setData({paramData:paramData})
        console.log("===that.data.receiveData====", that.data.receiveData,paramData)
        if (that.data.showFooter){
          that.setData({ showFooterState:false})
        }
        let jsonData='';
        try {
          jsonData = JSON.parse(paramData);
        } catch (e) {
          jsonData = paramData
        }
        that.setData({ authorizationCount: app.globalData.authorizationCount, footerCount: app.globalData.footerCount, showPopup: jsonData.state||false})
        console.log('zujian', paramData, that.data.showPopup, that.data.authorizationCount,app.globalData.loginUser)
        let userInfo=app.globalData.loginUser?{
            telno: app.globalData.loginUser?app.globalData.loginUser.telno:'',
            headimg: app.globalData.loginUser.userIcon||'',
            nickname: app.globalData.loginUser.nickname||'',
            userTip: app.globalData.loginUser.userTip||'',
            sex: app.globalData.loginUser.sex,
          }:{}
        that.setData({
          loginUser: app.globalData.loginUser,
          setting: app.globalData.setting,
          userInfo:userInfo,
        })
        console.log('setting', that.data.setting)
        console.log('app.globalData.loginUser', that.data.loginUser)
    // 获取用户信息
        if (that.data.canIUse) {
          console.log('==that.data.canIUse===', that.data.canIUse);
          app.globalData.userInfoReadyCallback = res => {
            console.log("=====userInfo====",res)
            that.setData({
              userInfo: res.userInfo,
            })
          }
        }
        that.getParac();
    },
    setShowBannerStateFun:function(state){
        let that=this;
        that.setData({showBannerState:state})
    },
    setPopupState:function(state){
        let that=this;
        that.setData({ showPopup:state })
    },
    toChangeUserInfo: function (userInfo) {
      let that = this;
      var customIndex = app.globalData.AddClientUrl("/change_user_info.html", userInfo, 'post')
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: app.globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log(res.data)
          if (res.data.errcode == '0') {
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            })
          }
          that.setData({ showUserForm: false })
          that.loginIn()
        },
        fail: function (res) {
          app.globalData.loadFail()
        },
        complete: function () {
          that.setData({ butn_show_loading: false })
        }
      })
    },

    loginIn: function (data) {
      app.globalData.get_session_userinfo()
      return
      console.log(data)
      var that = this;
      var loginUrl = app.globalData.AddClientUrl("Client.User.Login", data, 'post')
      wx.request({
        url: loginUrl.url,
        data: loginUrl.params,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.header)
          var header = res.header
          var cookie = null
          if (!!header['Set-Cookie']) {
            cookie = header['Set-Cookie']
          }
          if (!!header['set-cookie']) {
            cookie = header['set-cookie']
          }
          console.log(cookie)
          if (res.data.errcode == 0) {
            wx.setStorage({
              key: "cookie",
              data: cookie
            })
            app.globalData.header = {
              'content-type': 'application/json', // 默认值
              'Cookie': cookie
            }
            app.globalData.cookie = cookie
            app.globalData.loginUser = res.data.relateObj
            that.setData({ loginUser: res.data.relateObj })
            wx.setStorage({
              key: "loginUser",
              data: res.data.relateObj
            })
            wx.navigateBack()
          }
          else {
            wx.showToast({
              title: '失败',
              icon: 'loading',
              duration: 1500
            })
          }
        },
        fail: function (res) {
          console.log("fail")
          app.globalData.loadFail()
        }
      })
    },
    getDataFun: function (e) {
      let that = this;
      console.log("===getDataFun===", e, e.detail.formId)
      if (e.detail.formId) {
        let userInfo = that.data.userInfo;
        userInfo.userInfoFormCommitId = e.detail.formId
        that.toChangeUserInfo(userInfo)
      };
    },
    submitData: function (e) {
      let that = this;
      console.log("===getDataFun===", e, e.detail.formId)
      that.selectComponent("#userForm").formSubmit();
    },
    resEventFun: function (event){
      let that=this;
      console.log("==========resEventFun=========", event)
      let resEventData = event.detail.resEventData;
      that.triggerEvent('resEvent', { resEventData }, {})
    },
    
    bindGetUserInfo: function (e) {
      let that = this;
      that.setData({ showPopup: false })
      console.log(that.data.showPopup)
      console.log(e.detail.userInfo)
      if (e.detail.userInfo) {
        //用户按了允许授权按钮
        console.log('用户按了允许授权按钮')
        if (app.globalData.loginUser && app.globalData.loginUser.platformUser&&(!app.globalData.loginUser.platformUser.nickname||!app.globalData.loginUser.platformUser.headimgurl)) {
           app.globalData.sentWxUserInfo(app.globalData.loginUser,function(loginUser){
               that.triggerEvent('refreshFun',loginUser) //myevent自定义名称事件，父组件中使用
           })
        }else{
            that.triggerEvent('refreshFun',app.globalData.loginUser) //myevent自定义名称事件，父组件中使用
        }
      } else {
        console.log('用户按了拒绝按钮')
        //用户按了拒绝按钮
      }
    },
    cancel: function () {
      this.setData({ showPopup: false })
    },
    getParacFun: function (res) {
      let that = this;
      let url
      let jsonData;
      let params = { version: app.globalData.version };
      try {
        jsonData = JSON.parse(that.data.paramData);
        url = jsonData.url
      } catch (e) {
        jsonData = that.data.paramData
        console.log(e); 
        url = jsonData
      }
      if (jsonData.params) {
        params = Object.assign({}, params, jsonData.params)
      }
      if (res){
        params = Object.assign({}, params, res)
      }
      console.log("params", params)
      var customIndex = app.globalData.AddClientUrl("/custom_page_" + url + ".html", params, 'get', '1')
      wx.request({
        url: customIndex.url,
        header: app.globalData.header,
        success: function (res) {
          console.log("====== res.data=========", res.data)
          let data = res.data;
          if (!data.errcode | data.errcode == '0') {
            if (data.channelName != "index") {
              if (jsonData.title && jsonData.title == "noTitle") {
                console.log("======不设置标题=======")
              } else (
                wx.setNavigationBarTitle({
                  title: data.channelTitle,
                })
              )
            }
            wx.hideLoading()
            app.globalData.renderData = data
            that.setData({ renderData: data })
            if (data.partials.length == 0) {
              that.setData({ PaiXuPartials: null })
            } else {
              that.getPartials();
            }
          } else {
            console.log('加载失败')
          }
        },
        fail: function (res) {
          console.log('------------2222222-----------',res)
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '加载失败，点击【确定】重新加载',
            success: function (res) {
              if (res.confirm) {
                that.getParac()
              } else if (res.cancel) {
                app.globalData.toIndex()
              }
            }
          })
        }
      })
    },
    getParac: function () {
      let that = this;
      let params={}
      let locationAddressData = wx.getStorageSync('selectAddressData') || ''
      if (locationAddressData) {
        params = Object.assign({}, params, {
          "longitude": locationAddressData.longitude,
          "latitude": locationAddressData.latitude,
        })
        console.log("====params====已有经纬度", params,locationAddressData)
        that.getParacFun(params)
      } else {
        wx.getLocation({
          type: 'gcj02',
          success: function (res) {
            console.log("=====getLocationAddress1111111====", res)
            params.latitude = res.latitude
            params.longitude = res.longitude
            console.log("====params====获取当前地理位置", params)
            that.getParacFun(params)
          },
          fail: function (res) {
            console.log("fail",res)
            app.globalData.loadFail()
            that.getParacFun(params)
          }
        })
      }
    },
    getPartials: function () {
      let that=this;
      var partials = that.data.renderData.partials;
      console.log("=====partials=====", partials)
      var PaiXuPartials = [];
      //排序
      if (partials && partials.length) {
        for (let i = 0; i < partials.length; i++) {
          // 产品标签的转化为数组start
          if (partials[i].partialType == 6 && partials[i].androidTemplate=="footer-img"){
            console.log("====存在浮动图片====", that.data.footerImgState)
            that.setData({ footerImgState: true })
            console.log("====存在浮动图片====", that.data.footerImgState)
          }
          if (partials[i].partialType == 24 ){
            that.data.kefuCount++;
          }
          console.log('=========that.kefuCount=====', that.data.kefuCount)
          if (typeof (partials[i].jsonData) == "string") {
            partials[i].jsonData = JSON.parse(partials[i].jsonData)
          } else {
            continue;
          }

          console.log("=====partials=====", partials)
          PaiXuPartials.push(partials[i]);
        }
        wx.getSystemInfo({
          success: function (res) {
            let screenHeight = Math.floor(res.screenHeight * 0.618);
            let kefuHeight = Math.floor(that.data.kefuCount * ((65 + 20) / 2))
            console.log('===screenHeight====', screenHeight);
            let defaultTop = screenHeight - kefuHeight
            console.log('defaultTop', defaultTop)
            that.setData({
              defaultTop: defaultTop
            })
          },
        })
      }
      this.setData({ PaiXuPartials: PaiXuPartials })
      console.log(this.data.PaiXuPartials)
    },

  },
})
export default global['__wxComponents']['partials/custom_page/index']
</script>
<style platform="mp-weixin">
.authorization{
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  position: fixed;
  top: 0;
  width: 100%;
  height: 100%;
  z-index:10001;
}
.authorizationBg{
  position: fixed;
  top: 0;
  width: 100%;
  height: 100%;
  background: #000;
  opacity: 0.5;
  z-index:10000;
}
.authorizationPopup{
  width: 500rpx;
  height: 200rpx;
  background: #fff;
  border-radius:10rpx;
  display: flex;
  flex-direction: column;
  align-items: center; 
  justify-content: space-between
}
.buttonArray{
  display: flex;
  flex-direction: row;
  width: 100%
}
.tipContent{
  padding: 30rpx;
}
.button{
  border: 2rpx solid #ddd;
  width: 50%;
  border-radius: 0rpx;
  border-bottom: none;
}
.confirm{
  border-left: none;
  border-right: none;
}
.cancel{
  border-left: none;
}
.popupPage{
  position: fixed;
  display: flex;
  align-items: center;
  justify-content: center;
  bottom: 6%;
  left: 50%;
  width: 680rpx;
  height: 86%;
  margin-left: -340rpx;
  background: #fff;
  padding: 20rpx;
  z-index: 10002;
  box-sizing:border-box;
}
.submitBtn{
  position: fixed;
  display: flex;
  align-items: center;
  justify-content: center;
  bottom:7%;
  left: 50%;
  width: 650rpx;
  margin-left: -325rpx;
  color: #fff;
  padding: 20rpx;
  z-index: 10002;
  box-sizing:border-box;
  border-radius: 20rpx;
  font-size: 30rpx;
}
</style>